/**
 * ipassbook 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.dialog;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import org.apache.log4j.Logger;

import com.wateray.ipassbook.commom.Constant.Type;
import com.wateray.ipassbook.domain.Entity;
import com.wateray.ipassbook.domain.form.PassbookForm;
import com.wateray.ipassbook.domain.form.PassbookTradeForm;
import com.wateray.ipassbook.kernel.event.IPassbookEvent;
import com.wateray.ipassbook.kernel.event.IPassbookEventType;
import com.wateray.ipassbook.kernel.event.IPassbookObserver;
import com.wateray.ipassbook.kernel.service.PassbookService;
import com.wateray.ipassbook.kernel.service.PassbookTradeService;
import com.wateray.ipassbook.ui.model.PassbookComboBoxModel;
import com.wateray.ipassbook.ui.model.PassbookListCellRender;
import com.wateray.ipassbook.ui.model.TransferTableModel;
import com.wateray.ipassbook.ui.widget.LimitedDocument;
import com.wateray.ipassbook.ui.widget.TitleMessageDialog;
import com.wateray.ipassbook.util.CalendarUtils;
import com.wateray.ipassbook.util.LanguageLoader;
import com.wateray.ipassbook.util.log4j.Log;

/**
 * @author wateray
 * @create 2009-5-27
 */
public class TransferDialog extends TitleMessageDialog {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private static TransferDialog thisDailog;
	
	private static String AllowChar = ".0123456789";
    private static int Length = 15;

	/** logger. */
	Logger logger = Log.getLogger(this.getClass());

	/** layout data. */
	private static int width = 600;
	private static int height = 280;
	private static int centerIndent = 80;

	/** type radioButton. */
	private JLabel typeLabel;
	private ButtonGroup typeButtonGroup;
	private JRadioButton inRadioButton;
	private JRadioButton outRadioButton;

	/** date. */
	private JLabel dateLabel;
	private JTextField dateText;

	/** exchange rate. */
	private JLabel rateLabel;
	private JTextField rateText;

	// passbookComboBox List Items
	private Vector<Entity> passbookList;

	/** inPassbook. */
	private JLabel inPassbookLabel;
	private JComboBox inPassbookComboBox;
	private JTextField inCurrencyText;
	private PassbookComboBoxModel inPassbookComboBoxModel;

	/** amount. */
	private JLabel inAmountLabel;
	private JTextField inAmountText;

	/** amount. */
	private JLabel outAmountLabel;
	private JTextField outAmountText;

	/** passbook. */
	private JLabel outPassbookLabel;
	private JComboBox outPassbookComboBox;
	private JTextField outCurrencyText;
	private PassbookComboBoxModel outPassbookComboBoxModel;

	/** memo. */
	private JTextField memoText;

	/** bottom button. */
	private JCheckBox continueCheckBox;

	/** type. */
	private Type type;

	/** data. */
	private Object data;
//	private JTable transferTable;
//	private TransferTableModel transferTabelModel;
//	private int selectedIndex = -1;

	/** transferId. */
	private String transferId;
	
	/** transferForm. */
	private PassbookTradeForm transferForm;

	/**
	 * Constructor.
	 * 
	 * @param owner
	 *            -- JFrame. The parent of this dialog.
	 * @param type
	 *            -- Type.ADD; Type.MoDIFY; Type.DELET.
	 * */
	public TransferDialog(JFrame owner, Type type, Object data) {
		this(owner, width, height, true, "", type, data);
	}

	/**
	 * Constructor.
	 * 
	 * @param owner
	 *            -- JFrame. The parent of this dialog.
	 * @param title
	 *            -- The title.
	 * @param type
	 *            -- Type.ADD; Type.MoDIFY; Type.DELET.
	 * */
	public TransferDialog(Component owner, String title, Type type, Object data) {
		this(owner, width, height, true, title, type, data);

	}

	public TransferDialog(Component owner, int width, int height,
			boolean modal, String title, Type type, Object data) {
		super(owner, width, height, modal, title);
		this.type = type;
		setData(data);
		this.putButton(getContinueCheckBox());
		this.layoutButtonPanel();
		
		initData();
		initState();
	}

	@Override
	public JComponent getCenterPane() {
		if (centerPane == null) {
			centerPane = new JPanel(new GridBagLayout());
			GridBagConstraints c = new GridBagConstraints();
			typeLabel = new JLabel(LanguageLoader.getString("Receipt_Type"));

			c.fill = GridBagConstraints.BOTH;
			c.anchor = GridBagConstraints.PAGE_START;
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 0;
			c.gridwidth = 1;
			c.gridheight = 1;

			/** dataLabel. */
			dateLabel = new JLabel(LanguageLoader.getString("Date"));
			centerPane.add(dateLabel, c);

			// inRadioButton
			c.gridx = 1;
			c.gridwidth = 2;
			centerPane.add(getDateText(), c);
			c.gridwidth = 1;

			// spaceLabel
			c.gridx = 3;
			c.insets = new Insets(5, centerIndent, 5, 5);
			centerPane.add(new JLabel(), c);

			// rateSpinner
			rateLabel = new JLabel(LanguageLoader.getString("Rate"));
			c.insets = getDefaultInsets();
			c.gridx = 4;
			c.gridwidth = 1;
			centerPane.add(rateLabel, c);

			// rateSpinner
			c.gridx = 5;
			c.gridwidth = 2;
			centerPane.add(getRateText(), c);

			// outPassbookLabel
			outPassbookLabel = new JLabel(LanguageLoader
					.getString("outPassbook"));
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 1;
			c.gridwidth = 1;
			centerPane.add(outPassbookLabel, c);

			// outPassbookComboBox
			c.gridx = 1;
			c.insets = new Insets(5, 5, 5, 0);
			centerPane.add(getOutPassbookComboBox(), c);

			// outCurrencyText
			c.insets = new Insets(5, 0, 5, 5);
			c.gridx = 2;
			centerPane.add(getOutCurrencyText(), c);

			// inPassbookLabel
			inPassbookLabel = new JLabel(LanguageLoader.getString("inPassbook"));
			c.gridx = 4;
			c.gridwidth = 1;
			c.insets = getDefaultInsets();
			centerPane.add(inPassbookLabel, c);

			// inPassbookComboBox
			c.gridx = 5;
			c.insets = new Insets(5, 5, 5, 0);
			centerPane.add(getInPassbookComboBox(), c);

			// inCurrencyText
			c.insets = new Insets(5, 0, 5, 5);
			c.gridx = 6;
			centerPane.add(getInCurrencyText(), c);

			// outAmountLabel
			outAmountLabel = new JLabel(LanguageLoader.getString("outAmount"));
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 2;
			centerPane.add(outAmountLabel, c);

			// getDateText
			c.gridx = 1;
			c.gridwidth = 2;
			centerPane.add(getOutAmountText(), c);

			/** amount. */
			// inAmountLabel
			inAmountLabel = new JLabel(LanguageLoader.getString("inAmount"));
			c.gridx = 4;
			c.gridwidth = 1;
			centerPane.add(inAmountLabel, c);

			// inAmountText
			c.gridx = 5;
			c.gridwidth = 2;
			c.insets = new Insets(5, 5, 5, 5);
			centerPane.add(getInAmountText(), c);

			/** memo. */
			// memoLabel
			JLabel memoLabel = new JLabel(LanguageLoader.getString("Memo"));
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 3;
			c.gridwidth = 1;
			centerPane.add(memoLabel, c);

			// memoText
			c.gridx = 1;
			c.gridwidth = 6;
			centerPane.add(getMemoText(), c);

			/** initData. */
			initData();
		}
		return centerPane;

	}

	/**
	 * @param data
	 *            the data to set
	 */
	public void setData(Object data) {
		if(!(data instanceof PassbookTradeForm)){
			//throw new Exception();
		}
		this.data = data;
//		if(data instanceof JTable){
//			transferTable = (JTable) data;
//			transferTabelModel = (TransferTableModel) transferTable.getModel();
//			if(transferTable.getSelectedRow() != -1){
//				selectedIndex = transferTable.convertRowIndexToModel(transferTable.getSelectedRow());;
//			}
//		}
		
	}

	public void initData() {
		transferForm = null;

		/** -1 indicates no selection. */
		int noSelection = -1;

		if (getType() == Type.ADD) {
			transferForm = new PassbookTradeForm();
			// date
			getDateText().setText(
					CalendarUtils.getTodayInstance().getBeginDay());

			// rateText
			getRateText().setText("1.0");

			// outPassbookComboBox
			getOutPassbookComboBox().setSelectedIndex(noSelection);

			// inPassbookComboBox
			getInPassbookComboBox().setSelectedIndex(noSelection);

			// outAmount
			getOutAmountText().setText("");

			// onAmount
			getInAmountText().setText("");

			// memo
			getMemoText().setText("");
		} else if (getType() == Type.MODIFY || getType() == Type.DELETE) {
				transferForm = (PassbookTradeForm)data;
				// transferId
				transferId = transferForm.getTradeId();
				
				// date
				getDateText().setText(transferForm.getTradeDate());
				
				//exchange rate
				getRateText().setText(String.valueOf(transferForm.getTradeRate()));
						
				// outPassbookComboBox
				((PassbookComboBoxModel) getOutPassbookComboBox().getModel())
						.setSelectedObjectById(transferForm.getOutPassbookDetailId());
				
				// inPassbookComboBox
				((PassbookComboBoxModel) getInPassbookComboBox().getModel())
				.setSelectedObjectById(transferForm.getInPassbookDetailId());
		
				// currencyTex (It's not need to set this.)
				// getCurrencyText().setText(entity.getCurr_nm());

				// amount
				getOutAmountText().setText(String.valueOf(transferForm.getOutAmount()));
				getInAmountText().setText(String.valueOf(transferForm.getInAmount()));

				// memo
				getMemoText().setText(transferForm.getMemo());
		}

		// continueCheckBox
//		if (getType() != Type.ADD) {
//			getContinueCheckBox().setSelected(false);
//		} else {
//			getContinueCheckBox().setSelected(true);
//		}
		getContinueCheckBox().setSelected(false);
	}

	/** initState. */
	public void initState() {
		if (getType() == Type.ADD) {

		} else if (getType() == Type.MODIFY) {
			outPassbookComboBox.setEnabled(false);
			inPassbookComboBox.setEnabled(false);
		} else if (getType() == Type.DELETE) {
			for (Component componet : getCenterPane().getComponents()) {
				if (componet instanceof JLabel) {
					// Label needn't to disable. Do nothing.
				} else {
					componet.setEnabled(false);
				}
			}

			// invisible continueCheckBox
			getContinueCheckBox().setVisible(false);
		}
	}

	// process data.
	private void processData() {
		boolean flag = checkData();
		this.setMessageLabel(errorMessage);
		if (!flag) {
			return;
		}

		// process data start.

		// transferId
		transferForm.setTradeId(transferId);

		// date
		transferForm.setTradeDate(dateText.getText().trim());
		
		//rate getRateText
		transferForm.setTradeRate(Double.parseDouble(getRateText().getText().trim()));

		// inPassbook
		PassbookComboBoxModel inPassbookModel = (PassbookComboBoxModel) getInPassbookComboBox()
				.getModel();
		PassbookForm inPassbookForm = (PassbookForm) inPassbookModel
				.getSelectedObject();

		transferForm.setInPassbookId(inPassbookForm.getPassbookId());
		transferForm.setInPassbookSubId(inPassbookForm.getPassbookSubId());
		
		// outPassbook
		PassbookComboBoxModel outPassbookModel = (PassbookComboBoxModel) getOutPassbookComboBox()
				.getModel();
		PassbookForm outPassbookForm = (PassbookForm) outPassbookModel
				.getSelectedObject();

		transferForm.setOutPassbookId(outPassbookForm.getPassbookId());
		transferForm.setOutPassbookSubId(outPassbookForm.getPassbookSubId());
		
		// amount
		transferForm.setInAmount(Double.parseDouble(inAmountText.getText().trim()));
		
		//outAmount
		transferForm.setOutAmount(Double.parseDouble(outAmountText.getText().trim()));
		// memo
		transferForm.setMemo(memoText.getText());
		
		// // KBN
		// receipt
		// .setReceiptFlag(inRadioButton.isSelected() ? Constant.INCOME_KBN
		// : Constant.EXPENSE_KBN);
		// userID
		// inExEntity.setIn_ex_user_id("wateray");

		//
		PassbookTradeService servince = new PassbookTradeService();

		if (getType() == Type.ADD) {
			// transferId = inExDAO.insert(inExEntity);
			transferId = servince.insertPassbookTrade(transferForm);

			transferForm = servince.getPassbookTradeFormById(transferId);
			
			//invoke event.
			IPassbookObserver.getInstance().notifyEvents(
					new IPassbookEvent(this, IPassbookEventType.PassbookTableAddEvent));
//			// invoke transferTabelModel
//			transferTabelModel.addRow(servince.getPassbookTradeFormById(transferId));

		} else if (getType() == Type.MODIFY) {
			servince.updatePassbookTrade(transferForm);
			
			transferForm = servince.getPassbookTradeFormById(transferId);
			//invoke event.
			IPassbookObserver.getInstance().notifyEvents(
					new IPassbookEvent(this, IPassbookEventType.PassbookTableModifyEvent));
//			// invoke transferTabelModel
//			transferTabelModel.updateRow(selectedIndex,
//					servince.getPassbookTradeFormById(transferId));
		} else if (getType() == Type.DELETE) {
			servince.deletePassbookTrade(transferForm);
			//invoke event.
			
			IPassbookObserver.getInstance().notifyEvents(
					new IPassbookEvent(this, IPassbookEventType.PassbookTableDELETEEvent));
//			// invoke transferTabelModel
//			transferTabelModel.removeRow(selectedIndex);
		}

		// process data end.

		// Finished.
		if (!continueCheckBox.isSelected()) {
			// Close this dialog.
			this.dispose();
		} else {
			// Continue to add record.
			initData();
		}

	}

	private boolean checkData() {
		errorMessage = "";

		// check date
		// 1. empty
		if (getDateText().getText().trim().equals("")) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), dateLabel.getText());
			getDateText().requestFocus();
			return false;
		}
		// 2.Date format
		// try{
		// SimpleDateFormat.getInstance().parse(dateText.getText().trim());
		// }
		// catch(ParseException pe){
		// errorMessage = MessageFormat.format(LanguageLoader
		// .getString("DateParseException"), dateLabel.getText());
		// getDateText().requestFocus();
		// return false;
		// }
		
		// check passbook
		if (getOutPassbookComboBox().getSelectedItem() == null) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), outPassbookLabel.getText());
			getOutPassbookComboBox().requestFocus();
			return false;
		}
		
		// check inPassbook
		if (getInPassbookComboBox().getSelectedItem() == null) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), LanguageLoader
					.getString("CurrencyPassbook"));
			getInPassbookComboBox().requestFocus();
			return false;
		}
	
		// check outAmount
		// empty
		if (getOutAmountText().getText().trim().equals("")) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), outAmountLabel.getText());
			getOutAmountText().requestFocus();
			return false;
		}
		
		// number
		try {
			Double.parseDouble(getOutAmountText().getText().trim());
		} catch (NumberFormatException nfe) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("NumberFormatException"), outAmountLabel
					.getText());
			getOutAmountText().requestFocus();
			return false;
		}
		
		// check inAmount
		// empty
		if (getInAmountText().getText().trim().equals("")) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), inAmountLabel.getText());
			getInAmountText().requestFocus();
			return false;
		}
		
		// number
		try {
			Double.parseDouble(getInAmountText().getText().trim());
		} catch (NumberFormatException nfe) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("NumberFormatException"), inAmountLabel
					.getText());
			getInAmountText().requestFocus();
			return false;
		}
		
		//rate
		// empty
		if (getRateText().getText().trim().equals("")) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), rateLabel.getText());
			getRateText().requestFocus();
			return false;
		}
		
		// number
		try {
			Double.parseDouble(getRateText().getText().trim());
		} catch (NumberFormatException nfe) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("NumberFormatException"), rateLabel
					.getText());
			getRateText().requestFocus();
			return false;
		}

		return true;
	}

	private JTextField getDateText() {
		if (dateText == null) {
			dateText = new JTextField();
		}
		return dateText;
	}

	// rateSpinner
	private JTextField getRateText() {
		if (rateText == null) {
			rateText = new JTextField();
			LimitedDocument doc = new LimitedDocument(15,AllowChar);
			rateText.setDocument(doc);
			rateText.setHorizontalAlignment(JTextField.RIGHT);
			rateText.addKeyListener(new KeyAdapter(){
				
				public void keyReleased(KeyEvent e){
					// TODO Auto-generated method stub
					outAmountTextToInAmountText();		
				}
				
			});
		}
		return rateText;
	}

	// outCurrencyText
	private JTextField getOutCurrencyText() {
		if (outCurrencyText == null) {
			outCurrencyText = new JTextField();
			outCurrencyText.setEditable(false);
			// outCurrencyText.setMinimumSize(new Dimension(40, 0));
			outCurrencyText.setPreferredSize(new Dimension(40,
					(int) outCurrencyText.getPreferredSize().getHeight()));
		}

		return outCurrencyText;
	}

	// inCurrencyText
	private JTextField getInCurrencyText() {
		if (inCurrencyText == null) {
			inCurrencyText = new JTextField();
			inCurrencyText.setEditable(false);
			inCurrencyText.setPreferredSize(new Dimension(40,
					(int) inCurrencyText.getPreferredSize().getHeight()));
		}

		return inCurrencyText;
	}

	private JRadioButton getInRadioButton() {
		if (inRadioButton == null) {
			inRadioButton = new JRadioButton(LanguageLoader.getString("In"));
			inRadioButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					HandleActionPerformed(event);
				}
			});
			inRadioButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					HandleMouseClicked(e);
				}
			});

		}
		return inRadioButton;
	}

	private JRadioButton getOutRadioButton() {
		if (outRadioButton == null) {
			outRadioButton = new JRadioButton(LanguageLoader.getString("Out"));
			outRadioButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					HandleActionPerformed(event);
				}
			});
			outRadioButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					HandleActionPerformed(event);
				}
			});
			outRadioButton.doClick();
		}
		return outRadioButton;
	}

	private JComboBox getInPassbookComboBox() {
		if (inPassbookComboBox == null) {
			inPassbookComboBox = new JComboBox();
			inPassbookComboBox.setPreferredSize(new Dimension(120,
					(int) inPassbookComboBox.getPreferredSize().getHeight()));

			inPassbookComboBox.setModel(getInPassbookComboBoxModel());
			inPassbookComboBox.setRenderer(new PassbookListCellRender());
			inPassbookComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					passbookComboBoxActionPerformed(event);
				}
			});
		}

		return inPassbookComboBox;
	}

	private JTextField getInAmountText() {
		if (inAmountText == null) {
			inAmountText = new JTextField();
			LimitedDocument doc = new LimitedDocument(15,AllowChar);
			inAmountText.setDocument(doc);
			inAmountText.setHorizontalAlignment(JTextField.RIGHT);
			inAmountText.addKeyListener(new KeyAdapter(){
				
				public void keyReleased(KeyEvent e){
					// TODO Auto-generated method stub
					updateRate();	
				}
				
			});
		}
		return inAmountText;
	}

	private JTextField getOutAmountText() {
		if (outAmountText == null) {
			outAmountText = new JTextField();
			LimitedDocument doc = new LimitedDocument(Length,AllowChar);
			outAmountText.setDocument(doc);
			outAmountText.setHorizontalAlignment(JTextField.RIGHT);
			outAmountText.addKeyListener(new KeyAdapter(){
				public void keyReleased(KeyEvent e){
					outAmountTextToInAmountText();			
				}
			});
		}
		return outAmountText;
	}

	private PassbookComboBoxModel getInPassbookComboBoxModel() {
		if (inPassbookComboBoxModel == null) {
			inPassbookComboBoxModel = new PassbookComboBoxModel();
			inPassbookComboBoxModel.setData(getPassbookList());
		}
		return inPassbookComboBoxModel;
	}

	private JComboBox getOutPassbookComboBox() {
		if (outPassbookComboBox == null) {
			outPassbookComboBox = new JComboBox();
			outPassbookComboBox.setPreferredSize(new Dimension(120,
					(int) outPassbookComboBox.getPreferredSize().getHeight()));
			outPassbookComboBox.setModel(getOutPassbookComboBoxModel());
			outPassbookComboBox.setRenderer(new PassbookListCellRender());

			outPassbookComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					passbookComboBoxActionPerformed(event);
				}
			});
		}
		return outPassbookComboBox;
	}

	private PassbookComboBoxModel getOutPassbookComboBoxModel() {
		if (outPassbookComboBoxModel == null) {
			outPassbookComboBoxModel = new PassbookComboBoxModel();
			outPassbookComboBoxModel.setData(getPassbookList());
		}
		return outPassbookComboBoxModel;
	}

	private Vector<Entity> getPassbookList() {
		if (passbookList == null) {
			passbookList = new Vector();
			PassbookService service = new PassbookService();
			List result = service.getPassbookFormList();
			Iterator e = result.iterator();
			while (e.hasNext()) {
				passbookList.add((Entity) e.next());
			}
		}
		return passbookList;
	}

	private JCheckBox getContinueCheckBox() {
		if (continueCheckBox == null) {
			continueCheckBox = new JCheckBox(LanguageLoader
					.getString("Continue_Add"));
			// continueCheckBox.doClick();
		}
		return continueCheckBox;
	}

	private JTextField getMemoText() {
		if (memoText == null) {
			memoText = new JTextField();
		}
		return memoText;
	}

	private Insets getDefaultInsets() {
		return new Insets(5, 5, 5, 5);
	}

	private void HandleActionPerformed(ActionEvent event) {
		Handler(event.getSource());
	}

	private void HandleMouseClicked(MouseEvent event) {
		Handler(event.getSource());
	}

	private void Handler(Object source) {
		if (source.equals(inRadioButton)) {
		} else if (source.equals(outRadioButton)) {
		}
	}

	private void passbookComboBoxActionPerformed(ActionEvent event) {
		Object source = event.getSource();
		Vector<Entity> acopy = (Vector<Entity>) getPassbookList().clone();

		if (source.equals(getOutPassbookComboBox())) {
			PassbookForm pb = (PassbookForm) outPassbookComboBoxModel
					.getSelectedObject();
			if (pb != null) {
				outCurrencyText.setText(pb.getCurrencyName());
				acopy.remove(outPassbookComboBoxModel.getSelectedObject());
				inPassbookComboBoxModel.setData(acopy);
			} else {
				outCurrencyText.setText("");
			}
		} else if (source.equals(getInPassbookComboBox())) {
			PassbookForm pb = (PassbookForm) inPassbookComboBoxModel
					.getSelectedObject();
			if (pb != null) {
				inCurrencyText.setText(pb.getCurrencyName());
				// acopy.remove(inPassbookComboBoxModel.getSelectedObject());
				// outPassbookComboBoxModel.setData(acopy);
			} else {
				inCurrencyText.setText("");
			}
		}

	}

	/**
	 * @return the type
	 */
	public Type getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(Type type) {
		this.type = type;
	}
	
	/**
	 * @return transferForm -- PassbookTradeForm
	 * */
	public PassbookTradeForm getData(){
		return transferForm;
	}

	//Paste
	private void outAmountTextToInAmountText(){
		// number
		try {
			String outAmountStr = getOutAmountText().getText().trim();
			double outAmount = Double.parseDouble(outAmountStr);
			double rate = Double.parseDouble(getRateText().getText().trim());
			String inAmountStr = String.valueOf(outAmount * rate);
			int endIndex = inAmountStr.length()<Length? inAmountStr.length():Length;
			
			getInAmountText().setText(inAmountStr.substring(0, endIndex));
		} catch (NumberFormatException nfe) {
			//nfe.printStackTrace();
		}
	}
	
	//
	private void updateRate(){
		// number
		try {
			String outAmountStr = getOutAmountText().getText().trim();
			double outAmount = Double.parseDouble(outAmountStr);
			String inAmountStr = getInAmountText().getText().trim();
			double inAmount = Double.parseDouble(inAmountStr);
			
			double rate = inAmount/outAmount;
			String strRate = String.valueOf(rate);
			
			int endIndex = strRate.length()<Length? strRate.length():Length;
			
			getRateText().setText(strRate.substring(0, endIndex));
		} catch (NumberFormatException nfe) {
			//nfe.printStackTrace();
		}
	}
	
	@Override
	protected void finishButtonActionPerformed(ActionEvent event) {
		processData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wateray.ipassbook.kernel.event.IPassbookListener#update(com.wateray
	 * .ipassbook.kernel.event.IPassbookEvent)
	 */
	@Override
	public void update(IPassbookEvent event) {
		// TODO Auto-generated method stub

	}
}
